<?php

namespace app\api\controller;

use app\api\validate\Goods as GoodsValidate;
use app\BaseController;
use app\common\facade\auth\Auth;
use app\model\CouponBrand;
use app\model\CouponGoods;
use app\model\CouponOrder;
use app\model\RsRelate;
use think\Model;
use think\Request;

class Goods extends BaseController
{
    //use \app\api\controller\traits\coupon_order\Coupon;

    //2022年4月14日10:24:13 修改成同目录的 ProductChannel.php ,这个废掉了
    protected $middleware = [
         \app\middleware\ApiChannel::class,
         \app\middleware\ApiUserAuthV2::class => ['except' => ['phoneInfo']],
    ];

    protected function initialize()
    {
//        $this->request->servType = PRODUCT_COUPON_SERV;
//        $this->request->servName = '权益卡';
        parent::initialize();
    }

    /**
     * 首页推荐
     */
    public function recommend(Request $request)
    {
        $res = RsRelate::getRecommend();

        return success('成功！', $res);
    }

    /**
     * 顶部推荐
     */
    public function top(Request $request)
    {
        $res = RsRelate::getTop();

        return success('成功！', $res);
    }

    //视频充值
    public function payGoods(Request $request)
    {
        $data = input();
        $validate = new GoodsValidate();
        if (!$validate->scene('pay_coupon')->check($data)) {
            return fail($validate->getError());
        }
        $userId = $request->user['id'];
        $channel = $request->agentChannel;
        $result = CouponOrder::addOrder($data['goods_id'], $userId, $data['pay_way'], 3, $data['account'], $data['num'], $channel, $data['order_name']);

        return $result;
    }

    //权益首页接口返回
    public function showCate()
    {
        $res = RsRelate::getBrand();

        return success('成功！', $res);
    }

    //权益商品详情
    public function showDetail()
    {
        $data = input();
        $res = CouponGoods::showDetail($data);

        return $res;
    }

    //权益订单列表
    public function orderList(Request $request)
    {
        $data = input();
        $data['serv_type'] = 3;
        $userId = $request->user['id'];
        $res = CouponOrder::orderList($data,$userId);

        return success('成功！', $res);
    }

    /**
     * h5 订单列表
     * 备注： 以后订单列表不要用这个 换成统一接口 /api/CouponOrder/unionList  2023.4.28
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function couponOrderList(Request $request)
    {
        $data = input();
        $userId = $request->user['id'];
        $data['user_id'] = $userId;
        $couponOrderModel = new CouponOrder();
        $res = $couponOrderModel->couponOrderListH5($data);
        return $res;
    }

    //权益订单详情
    public function orderDetail(Request $request)
    {
        $data = input();
        $userId = $request->user['id'];
        $res = CouponOrder::orderDetail($data,$userId);

        return $res;
    }

    /**
     * CPS 订单，根据订单号，获取下单链接
     */
    public function getProductUrl(Request $request)
    {
        $data = input();
        $data['agent_id'] = $request->agentChannel['agent_id'];
        $data['user_id'] = $request->user['id'];
        $res = CouponOrder::getProductUrl($data);
        if (false === $res) {
            return fail(CouponOrder::getError());
        }
        return success('获取成功', $res);
    }

    /**
     * 权益-----获取商品详情页信息
     * @access public
     * @returns \think\response\Json
     * 暂不使用
     */
    public function getProductChannlDetail(Request $request)
    {
        $user = $request->user;
        if (empty($user['id'])) {
            return fail('用户错误');
        }
        $brand_id = $request->param('brand_id');
        $agent_id = Auth::agentId();
        if (empty($brand_id)) {
            return fail('请传入品牌id');
        }
        //查询品牌是否存在
        $brand = CouponBrand::where('id', $brand_id)->findOrEmpty();
        if ($brand->isEmpty()) {
            return fail('品牌不存在');
        }
        $brandMode = new CouponBrand();
        $rows = $brandMode->getProductChannlDetail($brand_id, $agent_id);
        //$rows = $brandMode->getProductChannlDetailNew($brand_id, $agent_id);
        return success('请求成功！', compact('rows'));
    }
}
